<template>
	<div class="mall-content">
		<span class="title">商城开关</span>
		<el-switch
			:disabled="!hasPermission('90870119')"
			:value="mallSwitchStatus"
			active-color="#13ce66"
			:active-value="1"
			:inactive-value="0"
			@change="handleChangeSwitch"
		></el-switch>
	</div>
</template>

<script>
export default {
	data() {
		return {
			configType: 57, // 商城开关
			configId: 1,
			mallSwitchStatus: 0
		}
	},
	created() {
		this.getValueAddedConfigByType()
	},
	methods: {
		getValueAddedConfigByType() {
			const params = { types: [this.configType] }
			this.$api.getValueAddedConfigByTypesBend(params).then((res) => {
				if (res?.code === 200 && Array.isArray(res?.data)) {
					this.configId = res.data[0]?.id
					this.mallSwitchStatus = res.data[0]?.vals?.enableFlag
				}
			})
		},
		handleChangeSwitch(val) {
			this.$confirm(
				`${
					val
						? this.$t('dxn.confirm_start_up')
						: this.$t('dxn.confirm_close')
				}`,
				this.$t('common.components.tip'),
				{
					confirmButtonText: this.$t('common.sure'),
					cancelButtonText: this.$t('common.cancel'),
					type: 'warning'
				}
			)
				.then(() => {
					if (!this.configId) {
						this.$message.error(
							this.$t('dxn.service_manager.config_not_exist_tips')
						)
						return
					}
					const params = {
						id: this.configId,
						toggleVal: val
					}
					this.$api
						.updateConfigToggleBend(params)
						.then((res) => {
							if (res?.code === 200) {
								this.$message.success(
									val
										? this.$t('dxn.game_rule.open_success')
										: this.$t('dxn.game_rule.close_success')
								)
								this.getValueAddedConfigByType()
							}
						})
						.catch(() => {})
				})
				.catch(() => {
					this.$message({
						type: 'info',
						message: '已取消'
					})
				})
		}
	}
}
</script>

<style lang="scss" scoped>
.mall-content {
	margin: 40px;
	.title {
		margin-right: 20px;
	}
}
</style>
